package com.example.myapplication.Sqlitedatabase;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import java.sql.SQLException;

public class OrlSqlDatabase extends OrmLiteSqliteOpenHelper {


    static String DATABASENAME="database.db";
    //定义数据库的版本号，当数据库需要升级时进行更改
    public static final int DATABASE_VERSION = 1;
    //创建DetailDataOpenHelper实例
    private static OrlSqlDatabase instance;

    public static OrlSqlDatabase getInstance(Context context) {
        if(instance==null){
            OrlSqlDatabase.instance=new OrlSqlDatabase(context,DATABASENAME,null,DATABASE_VERSION);
        }
        return instance;
    }

    public OrlSqlDatabase(Context context, String databaseName, SQLiteDatabase.CursorFactory factory, int databaseVersion) {
        super(context, databaseName, factory, databaseVersion);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
        try {
            TableUtils.createTable(connectionSource, Q1Bean.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int i, int i1) {
        try {
            TableUtils.dropTable(connectionSource,Q1Bean.class,false);
            TableUtils.createTable(connectionSource, Q1Bean.class);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
